package io.openmessaging.utils;

import java.util.function.Supplier;

public interface ArrayUtils {
    /**
     * 类似Map.computeIfAbsent(),线程安全
     */
    static <E> E computeIfAbsent(E[] array, int index, Supplier<E> supplier) {
        E e = array[index];
        if (e == null) {
            synchronized (array) {
                if ((e = array[index]) == null)
                    array[index] = (e = supplier.get());
            }
        }
        return e;
    }
}
